<?php 
	$_SESSION['realpage'] = true;
?>
<!DOCTYPE HTML>
<?php 
require_once "./class.GeneralServices.php";
require_once "./class.User.php";
$error = false;
session_start();
?>
<html>
<head>

<?php require_once "./HtmlIncludes.php";?>

<?php 
$group = GeneralServices::getService()->getGroup($_SESSION['userObj']->uid, $_SESSION['gid']);
$gid;
if(isset($group))
{
	$gid = $group->gid;
}
else {
	$gid = "0";
}
?>
<title>Insert title here</title>
</head>
    <body onload="LoadGroupPage();">
    <script type="text/javascript">
    function LoadGroupPage()
    {
    	var ur = window.parent.document.URL;			
		if(ur.substring(ur.length - 15, ur.length) != "Pages/panes.php")
			window.location.replace("./panes.php");
    	LoadGroupMembers();
    	LoadGroupWBMembers();
    	ShowMorePosts(5,0);
    }
    function Post(topic, content)
    {
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
    			    	$('#publishTopic').val('');
    			    	$('#publishContent').val('');
    			    	if($('#lastPostShown').val() != null)
    			    	{
    			    		ShowMorePosts(1,$('#lastPostShown').val());
    			    	}
    			    	else
    			    	{
    			    		ShowMorePosts(1,0);
    			    		$('#NoMorePostsError').css('visibility', 'hidden');
    				    	$('#NoMorePostsError').css('display', 'none');
    				    	$('#showMorePosts').css('display', 'block');
    				    	$('#showMorePosts').css('visibility', 'visible');
    			    	}
    			    	
    			    }
    			}
    		};	
    		xmlhttp.open("GET","./ajax.php?method=Post&sid="+"<?php echo session_id(); ?>"+"&gid="+$('#GID').val()+"&content="+replaceBR(content)+"&topic="+topic,true);
    		xmlhttp.send();
    	}

    function ShowMorePosts(limit, downPour)
    {
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
        			    if($('#lastPostShown').val() != null && xmlhttp.responseText != "")
        			    {
    			    		$('#lastPostShown').remove();
        			    }
    				    $('#postsDiv').append(xmlhttp.responseText);
    				    if(xmlhttp.responseText == "")
    				    {
    				    	$('#NoMorePostsError').css('visibility', 'visible');
    				    	$('#NoMorePostsError').css('display', 'block');
    				    	$('#showMorePosts').css('display', 'none');
    				    	$('#showMorePosts').css('visibility', 'hidden');
    				    	
    				    }
    			    }
    			}
    		};	
    		xmlhttp.open("GET","./ajax.php?method=getMorePosts&sid="+"<?php echo session_id(); ?>"+"&limit="+limit+"&downpour="+downPour+"&gid="+$('#GID').val()+"&relation="+$('#REL').val(),true);
    		xmlhttp.send();
    		return true;
    	}     

    function ShowMoreTKBKS(pid, limit, downPour)
    {
    		if (window.XMLHttpRequest)
    		{// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp=new XMLHttpRequest();
    		}
    		else
    		{// code for IE6, IE5
    			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		xmlhttp.onreadystatechange=function()
    		{
    			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		    {
    			    if(xmlhttp.responseText == 'false')
    			    {
    					alert('false');
    			    }
    			    else
    			    {
    			    	if($("#"+pid+"lastTKBKShown") != null && xmlhttp.responseText != "")
        			    {
    			    		$("#"+pid+"lastTKBKShown").remove();
        			    }    		
    			    	$("#"+pid+"TKBKSdiv").append(xmlhttp.responseText);    				    
    				    
    				    if(xmlhttp.responseText == "")
    				    {
    				    	$("#"+pid+"NoMoreTKBKsError").css('visibility', 'visible');
    				    	$("#"+pid+"NoMoreTKBKsError").css('display', 'block');
    				    	$("#"+pid+"showMoreTKBKs").css('display', 'none');
    				    	$("#"+pid+"showMoreTKBKs").css('visibility', 'hidden');
    				    	
    				    }
    				    $('#showMoreTKBKs').button();
    			    }
    			}
    		};	
    		xmlhttp.open("GET","./ajax.php?method=getTKBKs&sid="+"<?php echo session_id(); ?>"+"&pid="+pid+"&limit="+limit+"&downpour="+downPour,true);
    		xmlhttp.send();
    	}
    	function PostTKBK(pid, content)
        {
        		if (window.XMLHttpRequest)
        		{// code for IE7+, Firefox, Chrome, Opera, Safari
        			xmlhttp=new XMLHttpRequest();
        		}
        		else
        		{// code for IE6, IE5
        			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        		}
        		xmlhttp.onreadystatechange=function()
        		{
        			if (xmlhttp.readyState==4 && xmlhttp.status==200)
        		    {
        			    if(xmlhttp.responseText == 'false')
        			    {
        					alert('false');
        			    }
        			    else
        			    {
        			    	$("#"+pid+"publishTKBKContent").val('');
        			    	if($("#"+pid+"lastTKBKShown").val() != null)
        			    	{
        			    		ShowMoreTKBKS(pid, 1, $("#"+pid+"lastTKBKShown").val());
        			    	}
        			    	else
        			    	{
        			    		ShowMoreTKBKS(pid, 1, 0);
        			    	}        			    	
        			    }
        			}
        		};	
        		xmlhttp.open("GET","./ajax.php?method=PostTKBK&sid="+"<?php echo session_id(); ?>"+"&content="+replaceBR(content)+"&pid="+pid,true);
        		xmlhttp.send();
        	}
    	
    	function erasePost(pid)
        {
        		if (window.XMLHttpRequest)
        		{// code for IE7+, Firefox, Chrome, Opera, Safari
        			xmlhttp=new XMLHttpRequest();
        		}
        		else
        		{// code for IE6, IE5
        			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        		}
        		xmlhttp.onreadystatechange=function()
        		{
        			if (xmlhttp.readyState==4 && xmlhttp.status==200)
        		    {
        			    if(xmlhttp.responseText == 'false')
        			    {
        					alert('false');
        			    } 
        			    else
        			    {
            			    $("#"+pid+"showhide").remove();
            			    $("#"+pid+"erasePost").remove();
            			    $("#"+pid+"showhideContainer").prepend("<span class=\"ui-state-default ui-corner-all\" style=\"color: red;\" onclick=\"showPost("+pid+")\" id=\""+pid+"showPost\">Show</span>");            			   
            			}       			    
        			}
        		};	
        		xmlhttp.open("GET","./ajax.php?method=hidePost&sid="+"<?php echo session_id(); ?>"+"&pid="+pid,true);
        		xmlhttp.send();
        	}
    	
    	function showPost(pid)
        {
        		if (window.XMLHttpRequest)
        		{// code for IE7+, Firefox, Chrome, Opera, Safari
        			xmlhttp=new XMLHttpRequest();
        		}
        		else
        		{// code for IE6, IE5
        			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        		}
        		xmlhttp.onreadystatechange=function()
        		{
        			if (xmlhttp.readyState==4 && xmlhttp.status==200)
        		    {
        			    if(xmlhttp.responseText == 'false')
        			    {
        					alert('false');
        			    }  
        			    else
        			    {
        			    	$("#"+pid+"showhide").remove();
        			    	$("#"+pid+"showPost").remove();
            			    $("#"+pid+"showhideContainer").prepend("<span class=\"ui-state-default ui-corner-all\" style=\"color: red;\" onclick=\"erasePost("+pid+")\" id=\""+pid+"erasePost\">Hide</span>");
        			    }      			    
        			}
        		};	
        		xmlhttp.open("GET","./ajax.php?method=showPost&sid="+"<?php echo session_id(); ?>"+"&pid="+pid,true);
        		xmlhttp.send();
        	}
    	
        	function LoadGroupMembers()
            {
            		if (window.XMLHttpRequest)
            		{// code for IE7+, Firefox, Chrome, Opera, Safari
            			xmlhttp1=new XMLHttpRequest();
            		}
            		else
            		{// code for IE6, IE5
            			xmlhttp1=new ActiveXObject("Microsoft.XMLHTTP");
            		}
            		xmlhttp1.onreadystatechange=function()
            		{
            			if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
            		    {
            			    if(xmlhttp1.responseText == 'false')
            			    {
            					alert('false');
            			    }  
            			    else
            			    {
            			    	$("#groupMembersList").append(xmlhttp1.responseText);
            			    }      			    
            			}
            		};	
            		xmlhttp1.open("GET","./ajax.php?method=loadGroupMembers&sid="+"<?php echo session_id(); ?>"+"&gid="+"<?php echo $group->gid; ?>",true);
            		xmlhttp1.send();
            		return true;
            	}

        	function LoadGroupWBMembers()
            {
            		if (window.XMLHttpRequest)
            		{// code for IE7+, Firefox, Chrome, Opera, Safari
            			XMLWB=new XMLHttpRequest();
            		}
            		else
            		{// code for IE6, IE5
            			XMLWB=new ActiveXObject("Microsoft.XMLHTTP");
            		}
            		XMLWB.onreadystatechange=function()
            		{
            			if (XMLWB.readyState==4 && XMLWB.status==200)
            		    {
            			    if(XMLWB.responseText == 'false')
            			    {
            					alert('false');
            			    }  
            			    else
            			    {
            			    	$("#groupWBMembersList").append(XMLWB.responseText);
            			    }      			    
            			}
            		};	
            		XMLWB.open("GET","./ajax.php?method=loadGroupWBMembers&sid="+"<?php echo session_id(); ?>"+"&gid="+"<?php echo $group->gid; ?>",true);
            		XMLWB.send();
            		return true;
            	}
        	
        	function SearchFriend(creteria, limit)
        	{
        		if(creteria != null && creteria != '')
        		{
        			if (window.XMLHttpRequest)
        			{// code for IE7+, Firefox, Chrome, Opera, Safari
        				xmlhttp=new XMLHttpRequest();
        			}
        			else
        			{// code for IE6, IE5
        				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        			}
        			xmlhttp.onreadystatechange=function()
        			{
        				if (xmlhttp.readyState==4 && xmlhttp.status==200)
        			    {
        				    if(xmlhttp.responseText == 'false')
        				    {
        						alert('false');
        				    }
        				    else
        				    {
        					    if(xmlhttp.responseText != "")
        					    {
        					    	$('#friendSearchResults').css('visibility', 'visible');
        					    	$('#friendSearchResults').css('display', 'block');
        					    	$('#friendSearchResults').html(xmlhttp.responseText);
        					    }else
        					    {
        					    	$('#friendSearchResults').css('visibility', 'visible');
        					    	$('#friendSearchResults').css('display', 'block');
        					    	$('#friendSearchResults').html('Oops! We could not find any groups...');
        					    }
        				    }
        				}
        			};
        			xmlhttp.open("GET","./ajax.php?method=searchFriend&sid="+"<?php echo session_id(); ?>"+"&creteria="+creteria+"&limit="+limit+"&invite=true",true);
        			xmlhttp.send();
        		}
        	}

        	function InviteToGroup(uid, val)
        	{        		
        			if (window.XMLHttpRequest)
        			{// code for IE7+, Firefox, Chrome, Opera, Safari
        				xmlhttpInviteToGroup=new XMLHttpRequest();
        			}
        			else
        			{// code for IE6, IE5
        				xmlhttpInviteToGroup=new ActiveXObject("Microsoft.xmlhttpInviteToGroup");
        			}
        			xmlhttpInviteToGroup.onreadystatechange=function()
        			{
        				if (xmlhttpInviteToGroup.readyState==4 && xmlhttpInviteToGroup.status==200)
        			    {
        				    if(xmlhttpInviteToGroup.responseText == 'false')
        				    {
        						alert('false');
        				    }
        				    else
        				    {
            				    if(val == "true")
            				    {
									$("#"+uid+"invite").css("display", "none");
									$("#"+uid+"invite").css('visibility', "hidden");

									$("#"+uid+"uninvite").css('display', 'block');
									$("#"+uid+"uninvite").css('visibility', 'visible');
            				    }
            				    else
            				    {
            				    	$("#"+uid+"uninvite").css('display', 'none');
									$("#"+uid+"uninvite").css('visibility', 'hidden');

									$("#"+uid+"invite").css('display', 'block');
									$("#"+uid+"invite").css('visibility', 'visible');
            				    }        					    
        				    }
        				}
        			};
        			xmlhttpInviteToGroup.open("GET","./ajax.php?method=inviteToGroup&sid="+"<?php echo session_id(); ?>"+"&gid="+<?php echo $gid; ?>+"&uid="+uid+"&val="+val,true);
        			xmlhttpInviteToGroup.send();
        	} 
        	function goToUserPage(uid)
        	{        		
        			if (window.XMLHttpRequest)
        			{// code for IE7+, Firefox, Chrome, Opera, Safari
        				xmlhttp=new XMLHttpRequest();
        			}
        			else
        			{// code for IE6, IE5
        				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        			}
        			xmlhttp.onreadystatechange=function()
        			{
        				if (xmlhttp.readyState==4 && xmlhttp.status==200)
        			    {
        				    if(xmlhttp.responseText == 'false')
        				    {
        						alert('false');
        				    }
        				    else
        				    {
        				    	window.location = "./PrivateDetailsPage.php";      					    
        				    }
        				}
        			};
        			xmlhttp.open("GET","./ajax.php?method=goToUserPage&sid="+"<?php echo session_id(); ?>"+"&uid="+uid,true);
        			xmlhttp.send();
        	}   
        	function applyToGroup()
        	{        		
        			if (window.XMLHttpRequest)
        			{// code for IE7+, Firefox, Chrome, Opera, Safari
        				xmlhttp2=new XMLHttpRequest();
        			}
        			else
        			{// code for IE6, IE5
        				xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        			}
        			xmlhttp2.onreadystatechange=function()
        			{
        				if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
        			    {
        				    if(xmlhttp2.responseText == 'false')
        				    {
        						alert('false');
        				    }
        				    else
        				    {
        				    	     $('#applyMsg').css('visibility', 'visible');
        				    	     $('#applyMsg').css('display', 'block');
        				    	     $('#applyToGroupBtn').css('visibility', 'hidden');
        				    	     $('#applyToGroupBtn').css('display', 'none');   
        				    }
        				}
        			};
        			xmlhttp2.open("GET","./ajax.php?method=applyToGroup&sid="+"<?php echo session_id(); ?>"+"&gid="+$('#GID').val(),true);
        			xmlhttp2.send();
        	}  	
        	function friendMe(val)
        	{        		
        			if (window.XMLHttpRequest)
        			{// code for IE7+, Firefox, Chrome, Opera, Safari
        				xmlhttp2=new XMLHttpRequest();
        			}
        			else
        			{// code for IE6, IE5
        				xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
        			}
        			xmlhttp2.onreadystatechange=function()
        			{
        				if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
        			    {
        				    if(xmlhttp2.responseText == 'false')
        				    {
        						alert('false');
        				    }
        				    else
        				    {
        				    	     $('#friendMsg').css('visibility', 'visible');
        				    	     $('#friendMsg').css('display', 'block');
        				    	     $('#friendBtn').css('visibility', 'hidden');
        				    	     $('#friendBtn').css('display', 'none');   
        				    }
        				}
        			};
        			xmlhttp2.open("GET","./ajax.php?method=FriendUnFriend&sid="+"<?php echo session_id(); ?>"+"&uid="+"<?php echo $group->friendsgroup;?>"+"&val="+val,true);
        			xmlhttp2.send();
        	} 
    </script>
    	<div class="PaneHeader">
    		<?php echo $group->name;?>
    		<span style="color: red; font-size: .5em;"><?php if($group->Visible == '0') echo "group is invisible!"?></span>
    	</div> 
    	<input type="hidden" id="GUID" value="<?php echo $_SESSION['userObj']->uid;?>"/>
    	<input type="hidden" id="GID" value="<?php echo $group->gid;?>"/>
    	<input type="hidden" id="REL" value="<?php echo $group->relation;?>"/>   	    	  		       			
		<div><p style="background-size: contain; height: auto; min-height: 30px; width: 100%;" class="">Group description: <?php echo $group->desc;?></p></div>		
		<br>
		<div id="tabs">
		<ul>
			<li><a href="#tabs-1">Group's wall</a></li>
			<?php if($group->relation == "O") { echo '<li><a href="#tabs-2">Group Members</a></li>';} ?>
			<?php if($group->relation == "O") { echo '<li><a href="#tabs-3">Invite To Group</a></li>';} ?>
		</ul>
		<div id="tabs-1">
			<div id="postsDiv" class="MailItemFrame">
			<br>
			
			</div>		
			<span id="showMorePosts" onclick="ShowMorePosts(5,$('#lastPostShown').val())">Show more...</span>
			<div id="NoMorePostsError" style="visibility: hidden; display: none; color: red;">There are no more posts!</div>
			<br>
			<?php if($group->relation == "O" || $group->relation == "M") { echo '
			<div id="publishPostDiv" class="MailItemFrame">
				<form action="" method="POST">
					<span>Topic:<input id="publishTopic" type="text" style="margin-left: 21px; width: 133px;" class="text ui-widget-content ui-corner-all"/></span>
					<br>
					<span style="vertical-align: top;">Content:
						<textarea cols="16" rows="3" name="publishContent" id="publishContent" class="text ui-widget-content ui-corner-all"></textarea>
					</span>
				</form>
				<span id="publishPostButton" style="margin-left: 130px;" onclick="Post($(\'#publishTopic\').val(), $(\'#publishContent\').val())">Publish</span>
			</div>'; }?>
		</div>		
		<?php if($group->relation == "O"){ echo 
		'<div id="tabs-2">
			<div>
			<span>already members:</span>
			<br>				
				<div id="groupMembersList"></div>
			</div>			
			<div>
			<span>want to be members:</span>
			<br>				
				<div id="groupWBMembersList"></div>
			</div>
		</div>
		<div id="tabs-3">
			<div>				
				<span style="float: left;"><input id="friendsText" type="text" style="margin-left: 15px;" value="Search for a friend" onmouseover="RemoveText(\'friendsText\', \'Search for a friend\')" onmouseout="ReturnText(\'friendsText\', \'Search for a friend\')" onkeypress="fenterPressed(event)"/></span>
			    <div class="ui-state-default ui-corner-all" onclick="$(\'#friendSearchResults\').html(\'\'); $(\'#friendsText\').val(\'\'); ReturnText(\'friendsText\', \'Search for a friend\');"><span id="CloseSearchFriendButton">X</span></div>
			    <div id="friendSearchResults" style="visibility: hidden; display: none; width: 50%;">
			    
			    </div>				
			    <br>
			</div>
			    <script type="text/javascript">
			    function fenterPressed(e)
			    {
			    	if(e.keyCode == 13)
			    	{
			    		SearchFriend($(\'#friendsText\').val(), 4);
			    	}
			    }
			    </script>
		</div>';}?>
		<?php if($group->relation != "O" && $group->relation != "M" && $group->friendsgroup == "0") {?>
		<span id="applyToGroupBtn" onclick="applyToGroup()">apply to this group!</span>
		<span id="applyMsg" style="visibility: hidden; display: none;">you have applied for this group, <br> plaese wait for the owner to authorize you.</span>
		<?php } //closing bracket for apply to group button?>
		<?php if($group->relation != "O" && $group->relation != "M" && $group->friendsgroup != "0") {?>
		<span id="friendBtn" onclick="friendMe(true)">be a friend!</span>
		<span id="friendMsg" style="visibility: hidden; display: none;">you have applied for this friend, <br> plaese wait for the owner to authorize you.</span>
		<?php } //closing bracket for apply to group button?>
	</div>
<script type="text/javascript">
$( "#tabs" ).tabs({event: "mouseover"});
$('#publishPostButton').button();
$('#CloseSearchFriendButton').button();
$('#showMorePosts').button();
$('#applyToGroupBtn').button();
$('#friendBtn').button();
function togglePost(id)
{
	if($("#"+id).css('display') == 'none')
	{
		$("#"+id).css('visibility', 'visible');
		$("#"+id).css('display', 'block');
	}
	else
	{
		$("#"+id).css('visibility', 'hidden');
		$("#"+id).css('display', 'none');
	}
}
</script>
    </body>
</html>
<?php 
	$_SESSION['realpage'] = false;
?>